﻿using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TaskSchedul
{
    public class RabbitMqHelper
    {
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="msg"></param>
        public static void SendMessage(string message)
        {

            string HostName = ConfigurationManager.AppSettings["HostName"].ToString();
            string QueueName = ConfigurationManager.AppSettings["QueueName"].ToString(); ;
            int Port = int.Parse(ConfigurationManager.AppSettings["Port"].ToString());

            try
            {
                ConnectionFactory factory = new ConnectionFactory();
                factory.HostName = HostName;
                factory.Port = Port;

                IConnection conn = factory.CreateConnection();

                IModel channel = conn.CreateModel();

                //在MQ上定义一个持久化队列，如果名称相同不会重复创建
                channel.QueueDeclare(QueueName, true, false, false, null);

                byte[] bytes = Encoding.UTF8.GetBytes(message);

                //设置消息持久化
                IBasicProperties properties = channel.CreateBasicProperties();
                properties.DeliveryMode = 2;
                channel.BasicPublish("", QueueName, properties, bytes);
                 
            }
            catch (Exception e)
            {
                ToolHelper.WriteLog("发送消息", e.ToString());
            }
        }

        /// <summary>
        /// 接收消息
        /// </summary>
        /// <returns></returns>
        public static void ReceiveMessages()
        {
            string HostName = ConfigurationManager.AppSettings["HostName"].ToString();
            string QueueName = ConfigurationManager.AppSettings["QueueName"].ToString(); ;
            int Port = int.Parse(ConfigurationManager.AppSettings["Port"].ToString());

            try
            {
                ConnectionFactory factory = new ConnectionFactory();
                factory.HostName = HostName;
                factory.Port = Port;

                using (var connection = factory.CreateConnection())
                {
                    using (var channel = connection.CreateModel())
                    {
                        channel.QueueDeclare(QueueName, true, false, false, null);

                        var consumer = new QueueingBasicConsumer(channel);
                        channel.BasicConsume(QueueName, true, consumer);

                        while (true)
                        {
                            var ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue();

                            var body = ea.Body;
                            var message = Encoding.UTF8.GetString(body);

                            Main.MessageProcessing(message);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                ToolHelper.WriteLog("接收消息", e.ToString());
            }
        }



    }
}
